package com.example.sonone.quarz;

import com.example.sonone.service.impl.SystemParamCache;
import com.example.sonone.service.impl.SystemParamCacheLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

//@Component
@Order(5)
public class MytestQuarz implements SchedulingConfigurer {

    @Autowired
    private SystemParamCacheLoader systemParamCacheLoader;

    private final DateTimeFormatter yyyyMMddHHmmssSSS = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");

    /**
     * 直接写定时  写死的
     */
//    @Scheduled(fixedRate=1000)
    @Scheduled(cron = Constant.c1)
    public void test() {
//        System.out.println(123);
    }


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        // 从数据库读取配置  这样配置，因为缓存已经读取进来了，所有每次的执行时间是一样的，要想动态配置(即一改数据库，就发生时间变化，
        //  每次读取的时间不同 将下列代码放入) 会比 Runner 先执行
//        systemParamCacheLoader.load();
//        String cron = SystemParamCache.getInstance().getParamValue("crontest");


        Runnable task = new Runnable() {
            @Override
            public void run() {
                //任务逻辑代码部分.
                System.out.println("I am going:" + LocalDateTime.now().format(yyyyMMddHHmmssSSS));
            }
        };
        Trigger trigger = new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                systemParamCacheLoader.loadCron();
                String cron = SystemParamCache.getInstance().getParamValueCron("test");
                //任务触发，可修改任务的执行周期.
                //每一次任务触发，都会执行这里的方法一次，重新获取下一次的执行时间
                CronTrigger trigger = new CronTrigger(cron);
                Date nextExec = trigger.nextExecutionTime(triggerContext);
                return nextExec;
            }
        };
        //注册任务
        taskRegistrar.addTriggerTask(task, trigger);



    }


    public static void main(String[] args) {

        DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(LocalDateTime.now());

        LocalDateTime parse = LocalDateTime.parse("2020-01-09 12:01:01", DATE_TIME);

        System.out.println(parse);

        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        LocalDateTime date1 = LocalDateTime.now();
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String date2Str = formatter2.format(date1);
        System.out.println(date2Str);

        System.out.println("============");
        LocalDateTime ldt = localDateTime.now();

        DateTimeFormatter yyyyMMddHHmmssSSS = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String format = ldt.format(yyyyMMddHHmmssSSS);
        System.out.println(format);



    }
}
